package net.reactor;

import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * 响应式流的发布者(发布0->1个元素)
 * <p>
 * * {@link Mono#create(Consumer)} 用MonoSink创建流
 * * {@link Mono#defer(Supplier)}  接收另一个Mono流的返回值作为新的流
 * * {@link Mono#delay(Duration)}  创建一个流,在指定的延迟时间之后,产生数字0作为唯一值
 * * {@link Mono#error(Throwable)} 创建一个包含异常的流
 *
 * @author 王霄
 * @date 2021/10/1 08:53
 */
public class MonoDemo {

    static Consumer<Object> consumer = System.out::println;

    public static void main(String[] args) throws InterruptedException {
        Mono.create(monoSink -> monoSink.success("create发布")).subscribe(consumer);

        Mono.defer(() -> Mono.just(1)).subscribe(consumer);

        Mono.delay(Duration.ofSeconds(3)).subscribe(consumer);

        Mono.error(new NullPointerException()).onErrorReturn("发生异常").subscribe(consumer);

        Thread.sleep(1000000000);
    }

}
